﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using WindowsLive.Writer.Api;
using ScottIsAFool;

namespace WordpressFeaturedImagePlugin
{
    public class PluginSettings
    {
        IProperties m_properties;
        private const string PASSWORD = "PASSWORD";
        private const string CHECKEDCATEGORIES = "CHECKEDCATEGORIES";
        private const string USEPOSTIDASIMAGENAME = "USEPOSTIDASIMAGENAME";

        private static readonly byte[] CryptKey = new byte[] { 4, 3, 4, 5, 6, 5 };

        public PluginSettings(IProperties properties)
        {
            m_properties = properties;
        }

        public string Password
        {
            get {
                string password = m_properties.GetString(PASSWORD, "");
                if (string.IsNullOrEmpty(password))
                {
                    return string.Empty;
                }
                else
                {
                    return Encoding.UTF8.GetString(ProtectedData.Unprotect(Convert.FromBase64String(password), CryptKey, DataProtectionScope.LocalMachine));
                }
            }
            set { m_properties.SetString(PASSWORD, Convert.ToBase64String(ProtectedData.Protect(Encoding.UTF8.GetBytes(value), CryptKey, DataProtectionScope.LocalMachine))); }
        }

        public string CheckedCategories
        {
            get { return m_properties.GetString(CHECKEDCATEGORIES, ""); }
            set { m_properties.SetString(CHECKEDCATEGORIES, value); }
        }

        public bool UsePostIdAsImageName
        {
            get { return m_properties.GetBoolean(USEPOSTIDASIMAGENAME, true); }
            set { m_properties.SetBoolean(USEPOSTIDASIMAGENAME, value); }
        }
    }
}
